package promotion

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"context"
)

// ListItemConfigurationPtrsByPromotionID 获取促销活动到商品配置数组
func (s *PromotionEntityServiceImp) ListItemConfigurationPtrsByPromotionID(ctx context.Context, q *sqlc.Queries, couponID int64) (arr param.ItemConfigurationPtrArray, err error) {
	couponItems, err := q.ListNormalPromotionItemsByPromotionID(ctx, couponID)
	if err != nil {
		return
	}
	for _, couponItem := range couponItems {
		arr = append(arr, &param.ItemConfiguration{
			ID:   couponItem.ItemID,
			Name: couponItem.ItemName,
		})
	}
	return
}

// GetMapOfPromotionIDToItemConfigurationPtrsByPromotionIDs 获取促销活动到商品配置的映射
func (s *PromotionEntityServiceImp) GetMapOfPromotionIDToItemConfigurationPtrsByPromotionIDs(ctx context.Context, q *sqlc.Queries, promotionIDs []int64) (m param.EntityIDToItemConfigurationPtrsMap, err error) {
	m = make(param.EntityIDToItemConfigurationPtrsMap)
	if len(promotionIDs) == 0 {
		return
	}
	promotionItems, err := q.ListNormalPromotionItemsByPromotionIDs(ctx, promotionIDs)
	if err != nil {
		return
	}
	for _, promotionItem := range promotionItems {
		promotionID := promotionItem.PromotionID
		valPtr := &param.ItemConfiguration{
			ID:   promotionItem.ItemID,
			Name: promotionItem.ItemName,
		}
		if val, ok := m[promotionID]; ok {
			val = append(val, valPtr)
			m[promotionID] = val
		} else {
			m[promotionID] = []*param.ItemConfiguration{valPtr}
		}
	}
	return
}

// GetMapOfPromotionIDToItemConfigurationPtrsByPromotionPtrs 获取促销活动到商品配置的映射
func (s *PromotionEntityServiceImp) GetMapOfPromotionIDToItemConfigurationPtrsByPromotionPtrs(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.Promotion) (m param.EntityIDToItemConfigurationPtrsMap, err error) {
	return s.GetMapOfPromotionIDToItemConfigurationPtrsByPromotionIDs(ctx, q, s.ConvToPromotionIDs(ptrs))
}

// GetMapOfPromotionIDToItemConfigurationPtrsByListPromotionWithStatRowPtrs 获取促销活动到商品配置的映射
func (s *PromotionEntityServiceImp) GetMapOfPromotionIDToItemConfigurationPtrsByListPromotionWithStatRowPtrs(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.ListPromotionWithStatsRow) (m param.EntityIDToItemConfigurationPtrsMap, err error) {
	return s.GetMapOfPromotionIDToItemConfigurationPtrsByPromotionIDs(ctx, q, s.ConvToPromotionIDs(ptrs))
}

// GetMapOfPromotionIDToItemIDsByPromotionIDs 获取促销活动到商品ID的映射
func (s *PromotionEntityServiceImp) GetMapOfPromotionIDToItemIDsByPromotionIDs(ctx context.Context, q *sqlc.Queries, promotionIDs []int64) (m param.EntityIDToItemIDMap, err error) {
	m = make(param.EntityIDToItemIDMap)
	if len(promotionIDs) == 0 {
		return
	}
	promotionItems, err := q.ListNormalPromotionItemsByPromotionIDs(ctx, promotionIDs)
	if err != nil {
		return
	}
	for _, promotionItem := range promotionItems {
		promotionID := promotionItem.PromotionID
		val := param.ItemID(promotionItem.ItemID)
		if vals, ok := m[promotionID]; ok {
			vals = append(vals, val)
			m[promotionID] = vals
		} else {
			m[promotionID] = []param.ItemID{val}
		}
	}
	return
}

// GetMapOfPromotionIDToItemIDsByPromotionPtrs 获取促销活动到商品ID的映射
func (s *PromotionEntityServiceImp) GetMapOfPromotionIDToItemIDsByPromotionPtrs(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.Promotion) (m param.EntityIDToItemIDMap, err error) {
	return s.GetMapOfPromotionIDToItemIDsByPromotionIDs(ctx, q, s.ConvToPromotionIDs(ptrs))
}